package com.dream.wukong.common.core.jdbc.sqlconfig.impl;

import com.dream.wukong.common.core.jdbc.exception.SqlConfigDuplicateException;
import com.dream.wukong.common.core.jdbc.sqlconfig.SqlConfig;
import com.dream.wukong.common.core.jdbc.sqlconfig.SqlMapper;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author: wuyichao
 * @Description:
 * @Date: Created in 10:18 2018/9/18.
 * @Modified:
 *
 * 说明：SQL配置基类，可根据项目实际需要进行扩展，
 * 比如将SQL配置信息存储到数据库中，从数据库中读取SQL信息
 * public class DBSqlConfig extends BaseSqlConfig {
 * 	 String Sql = "SELECT isUseCache ,isFlushCache, SqlKey ,SqlCode FROM SQL_CONFIG";
 *   public DBSqlConfig(){
 *		loadConfig();
 *	}
 *	void loadConfig(){
 *    //读取数据库
 *    for (final Object result : results) {
SqlMapper sqlMapper =  new SqlMapper (){
// 相关字段赋值

} ;
this.addSqlMapper(sqlMapper.getSqlKey(), sqlMapper);
}
 *  }
 *
 * }
 */
public abstract class BaseSqlConfig implements SqlConfig {

    protected Map<String, SqlMapper> sqlConfigCache = new HashMap<String, SqlMapper>();

    public BaseSqlConfig() {
        super();
    }

    public SqlMapper getSqlMapper(String sqlKey) {
        return sqlConfigCache.get(sqlKey);
    }

    public void addSqlMapper(String sqlKey, SqlMapper sqlMapper) {
        check(sqlKey);
        sqlConfigCache.put(sqlKey, sqlMapper);
    }

    protected void check(String sqlKey) {
        if (sqlConfigCache.containsKey(sqlKey)) {
            throw new SqlConfigDuplicateException("Duplicate sqlKey : "
                    + sqlKey);
        }
    }

    public void removeSqlMapper(String sqlKey) {
        sqlConfigCache.remove(sqlKey);
    }

    public Set<String> getKeyList() {
        return sqlConfigCache.keySet();
    }

    @Override
    public String toString() {
        return sqlConfigCache.toString();
    }

}
